/***************************************************************************
 * The Open Armory API is a project to retrieve World of Warcraft(R) 
 * related XML data from The Armory.
 * 
 * The data retrieved via the Open Armory API from The Armory is 
 * proprietary to Blizzard Entertainment, Inc(R).
 * *************************************************************************
 * Copyright (C) 2009 - 2010 Tedla M.B. Brandsema
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ***************************************************************************/
package net.openarmory.core.domain;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import net.openarmory.core.config.ConfigurationProperties;
import net.openarmory.core.domain.constants.ArmoryIconSize;
import net.openarmory.core.domain.constants.ArmoryPortraitLevel;
import net.openarmory.core.domain.constants.ArmoryZone;
import net.openarmory.core.domain.constants.CharacterClass;
import net.openarmory.core.domain.constants.CharacterGender;
import net.openarmory.core.domain.constants.CharacterRace;

import org.apache.log4j.Logger;

/**
 * The Class {@code ArmoryImageWriter} writes Image files retrieved via the
 * {@link ArmoryImageRetriever} to disk.
 */
public final class ArmoryImageWriter extends ArmoryBinaryWriter {

	/** The logger. */
	private static final Logger LOG = Logger.getLogger(ArmoryImageWriter.class);

	/** The image retriever. */
	private final ArmoryImageRetriever imageRetriever = new ArmoryImageRetriever();

	/** The configuration properties. */
	private ConfigurationProperties config;

	/**
	 * Instantiates a new armory image writer.
	 */
	public ArmoryImageWriter() {
		try {
			this.config = ConfigurationProperties.getInstance();
		} catch (final IOException e) {
			LOG.error("Can not open config file.", e);
		}
	}

	/**
	 * Writes the icon image to disk.
	 * 
	 * @param zone
	 *            The zone represents The Armory region (i.e. EU or US).
	 * @param iconSize
	 *            The size of the icon to retrieve (i.e. 21, 43 or 64 pixels).
	 * @param iconName
	 *            The name of the icon to retrieve.
	 */
	public void writeIcon(final ArmoryZone zone, final ArmoryIconSize iconSize,
			final String iconName) {
		final InputStream imageInputStream = this.imageRetriever.fetchIcon(
				zone, iconSize, iconName);
		final File outFile = createOutFile(this.imageRetriever.getArmoryUrl()
				.getUrl());
		writeBinaryFile(this.imageRetriever.getContentLengthHeader(),
				imageInputStream, outFile);
	}

	/**
	 * Writes the portrait to disk.
	 * 
	 * @param zone
	 *            The zone represents The Armory region (i.e. EU or US).
	 * @param portraitLevel
	 *            The portrait level represents the character level which is
	 *            associated with the portrait (i.e. 80, 70, 60 or default).
	 * @param gender
	 *            The gender of the portrait character.
	 * @param race
	 *            The race of the portrait character.
	 * @param clazz
	 *            The class of the portrait character.
	 */
	public void writePortrait(final ArmoryZone zone,
			final ArmoryPortraitLevel portraitLevel,
			final CharacterGender gender, final CharacterRace race,
			final CharacterClass clazz) {
		final InputStream imageInputStream = this.imageRetriever.fetchPortrait(
				zone, portraitLevel, gender, race, clazz);
		final File outFile = createOutFile(this.imageRetriever.getArmoryUrl()
				.getUrl());
		writeBinaryFile(this.imageRetriever.getContentLengthHeader(),
				imageInputStream, outFile);
	}

	/**
	 * Creates the file where the image will be written to.
	 * 
	 * @param url
	 *            The URL from where the image is taken forms the basis for the
	 *            file.
	 * @return The output file.
	 */
	private File createOutFile(final URL url) {
		return new File(this.config.getOutputDir() + File.separatorChar
				+ url.getPath());
	}
}
